package com.rightsidetech.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.mall.dto.request.userApp.CommodityAppPageReq;
import com.rightsidetech.mall.dto.request.userApp.OptCommodityReq;
import com.rightsidetech.mall.dto.response.userApp.*;
import com.rightsidetech.mall.entity.MallAgentCommodity;
import com.rightsidetech.mall.entity.MallCommodityWithBLOBs;
import com.rightsidetech.mall.mapper.MallAgentCommodityMapper;
import com.rightsidetech.mall.mapper.MallCommodityMapper;
import com.rightsidetech.mall.mapper.MallCommodityPictureMapper;
import com.rightsidetech.mall.mapper.MallCommoditySpecsMapper;
import com.rightsidetech.mall.service.MallUserAppCommodityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.math.BigDecimal;
import java.util.*;

@Service("mallUserAppCommodityService")
public class MallUserAppCommodityServiceImpl implements MallUserAppCommodityService {

    @Resource
    private MallCommodityMapper mallCommodityMapper;

    @Resource
    private MallCommoditySpecsMapper mallCommoditySpecsMapper;

    @Resource
    private MallCommodityPictureMapper mallCommodityPictureMapper;

    @Resource
    private MallAgentCommodityMapper mallAgentCommodityMapper;

    @Override
    public ResponseData<PageInfo<CommodityAppPageResp>> page(CommodityAppPageReq req,Long agentId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<CommodityAppPageResp> result = mallCommodityMapper.selectAppCommodityPage(req.getTypeId(),agentId);
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData<CommodityAppDetailResp> findDetailById(Long commodityId,Long agentId) {
        if(commodityId == null || commodityId < 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品id不可为空或是负数");
        }
        MallAgentCommodity agentCommodity = mallAgentCommodityMapper.selectByCommodityIdAndAgentId(commodityId, agentId);
        if(ObjectUtil.isEmpty(agentCommodity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品未上架到此门店");
        }
        CommodityAppDetailResp result = mallCommodityMapper.selectAppDetailById(commodityId);
        if (ObjectUtil.isEmpty(result)) {
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到该商品或该商品已下架");
        }
        List<BigDecimal> prices = mallCommoditySpecsMapper.selectPriceByCommodityId(commodityId);
        if(CollUtil.isNotEmpty(prices)){
            result.setSmallPrice(prices.get(0));
            if(prices.size() != 1){
                result.setBigPrice(prices.get(prices.size() - 1));
            }
        }
        result.setCommodityStatus(agentCommodity.getCommodityStatus());
        result.setCommodityDetailPicture(mallCommodityPictureMapper.selectPictureById(commodityId,(byte)1));
        result.setCommodityPicture(mallCommodityPictureMapper.selectPictureById(commodityId,(byte)0));
        return ResponseData.success(result);
    }

    @Override
    public ResponseData<OptCommodityOneResp> oneCommodity(Long commodityId) {
        if(ObjectUtil.isEmpty(commodityId) || commodityId < 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品id不可为空,或小于0");
        }
        MallCommodityWithBLOBs commodity = mallCommodityMapper.selectByPrimaryKey(commodityId);
        if(ObjectUtil.isEmpty(commodity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到该商品");
        }
        if(commodity.getCommodityStatus() != 3){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品状态不正确");
        }
        OptCommodityOneResp result = new OptCommodityOneResp();
        result.setPicture(commodity.getCover());
        result.setIsInNew(commodity.getIsInNew());
        result.setId(commodityId);
        if(StrUtil.isNotEmpty(commodity.getSpecsJson())){
            List<String> specs = mallCommoditySpecsMapper.selectSpecsByCommodityId(commodityId);
            List<UserAppSpecsResp> specsList = new ArrayList<>();
            for (String spec : specs) {
                specsList.add(new UserAppSpecsResp(spec));

            }
            result.setSpecsList(specsList);
        }
        List<BigDecimal> prices = mallCommoditySpecsMapper.selectPriceByCommodityId(commodityId);
        if(CollUtil.isNotEmpty(prices)){
            result.setSmallPrice(prices.get(0));
            result.setBigPrice(prices.get(prices.size() - 1));
        }
        return ResponseData.success(result);
    }

    @Override
    public ResponseData<OptCommodityResp> optCommodity(OptCommodityReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        MallCommodityWithBLOBs commodity = mallCommodityMapper.selectByPrimaryKey(req.getCommodityId());
        if(ObjectUtil.isEmpty(commodity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到商品");
        }
        if(commodity.getCommodityStatus() != 3){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品状态不正确");
        }
        if(StrUtil.isNotEmpty(req.getSpecs())){
            if(StrUtil.isEmpty(commodity.getSpecsJson())){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品没有规格");
            }
            OptCommodityResp result;
            if(req.getManner() == 1){
                if(commodity.getIsInNew() == 1){
                    return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品不支持以旧换新");
                }
                result = mallCommoditySpecsMapper.selectInNewByCommodityAndSpecsIds(commodity.getId(),req.getSpecs());
            }else {
                result = mallCommoditySpecsMapper.selectByCommodityAndSpecsIds(commodity.getId(),req.getSpecs());
            }
            if(ObjectUtil.isEmpty(result)){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到该规格商品");
            }
            return ResponseData.success(result);
        }
        if(StrUtil.isNotEmpty(commodity.getSpecsJson())){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品有规格请选择规格");
        }
        OptCommodityResp result = new OptCommodityResp();
        result.setPicture(commodity.getCover());
        if(req.getManner() == 1) {
            if (commodity.getIsInNew() == 1) {
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品不支持以旧换新");
            }
            result.setPrice(commodity.getInNewPrice());
        }else {
            result.setPrice(commodity.getPrice());
        }
        return ResponseData.success(result);
    }

    @Override
    public List<Long> selectCommodityIdsByAgentId(Long agentId) {
        return mallAgentCommodityMapper.selectCommodityIdByAgentId(agentId);
    }
}
